/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.wuhenzhizao.ohostitlebar.slice;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import org.wuhenzhizao.ohostitlebar.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import org.wuhenzhizao.ohostitlebar.adapter.MainPagerAdapter;
import org.wuhenzhizao.ohostitlebar.utils.ViewCreateHelper;
import org.wuhenzhizao.titlebar.widget.CustomTitleBar;

import java.io.IOException;
import java.util.ArrayList;

/**
 * TopAbilitySlice
 *
 * @since 2021-06-08
 */
public class TopAbilitySlice extends AbilitySlice implements Component.ClickedListener, TabList.TabSelectedListener, PageSlider.PageChangedListener {
    private DirectionalLayout mBank;
    private DependentLayout mDirectionalLayout;
    private TabList tabList;
    private PageSlider pageSlider;
    private Text mText;
    private Button mLeftBankFour;
    private Button mButton;
    private DependentLayout mDelPager;
    private CustomTitleBar mCustomTitleBar1;
    private CustomTitleBar mCustomTitleBar2;
    private CustomTitleBar mCustomTitleBar3;
    private CustomTitleBar mCustomTitleBar4;
    private CustomTitleBar mCustomTitleBar5;
    private CustomTitleBar mCustomTitleBar6;
    private CustomTitleBar mCustomTitleBar7;
    private CustomTitleBar mCustomTitleBar8;
    private CustomTitleBar mCustomTitleBar9;
    private CustomTitleBar mCustomTitleBar10;
    private CustomTitleBar mCustomTitleBar11;
    private DependentLayout mDependentLayoutFour;
    private int position;

    /**
     * onStart
     *
     * @param intent intent
     */
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_top);
        position = intent.getIntParam("position", 0);
        mBank = (DirectionalLayout) findComponentById(ResourceTable.Id_left_bank_dil);
        tabList = (TabList) findComponentById(ResourceTable.Id_tab_list);
        pageSlider = (PageSlider) findComponentById(ResourceTable.Id_pager);
        tabList.addTabSelectedListener(this);
        pageSlider.addPageChangedListener(this);
        tabList.setFixedMode(true);
        mDelPager = (DependentLayout) findComponentById(ResourceTable.Id_Del_pager);
        mDependentLayoutFour = (DependentLayout) findComponentById(ResourceTable.Id_DependentLayout_four);
        mCustomTitleBar1 = (CustomTitleBar) findComponentById(ResourceTable.Id_Top1);
        mCustomTitleBar2 = (CustomTitleBar) findComponentById(ResourceTable.Id_Top2);
        mCustomTitleBar3 = (CustomTitleBar) findComponentById(ResourceTable.Id_Top3);
        mCustomTitleBar4 = (CustomTitleBar) findComponentById(ResourceTable.Id_Top4);
        mCustomTitleBar5 = (CustomTitleBar) findComponentById(ResourceTable.Id_Top5);
        mCustomTitleBar6 = (CustomTitleBar) findComponentById(ResourceTable.Id_Top6);
        mCustomTitleBar7 = (CustomTitleBar) findComponentById(ResourceTable.Id_Top7);
        mCustomTitleBar8 = (CustomTitleBar) findComponentById(ResourceTable.Id_Top8);
        mCustomTitleBar9 = (CustomTitleBar) findComponentById(ResourceTable.Id_Top9);
        mCustomTitleBar10 = (CustomTitleBar) findComponentById(ResourceTable.Id_Top10);
        mCustomTitleBar11 = (CustomTitleBar) findComponentById(ResourceTable.Id_Top11);
        mDirectionalLayout = (DependentLayout) findComponentById(ResourceTable.Id_left_bank_dils);
        mLeftBankFour = (Button) findComponentById(ResourceTable.Id_left_bank_four);
        mButton = (Button) findComponentById(ResourceTable.Id_left_bank_buttons);
        mText = (Text) findComponentById(ResourceTable.Id_List_text);
        mCustomTitleBar1.setClickedListener(this);
        mCustomTitleBar2.setClickedListener(this);
        mCustomTitleBar3.setClickedListener(this);
        mCustomTitleBar4.setClickedListener(this);
        mCustomTitleBar5.setClickedListener(this);
        mCustomTitleBar6.setClickedListener(this);
        mCustomTitleBar7.setClickedListener(this);
        mCustomTitleBar8.setClickedListener(this);
        mCustomTitleBar9.setClickedListener(this);
        mCustomTitleBar10.setClickedListener(this);
        mCustomTitleBar11.setClickedListener(this);
        mText.scrollTo(0, 0);
        initData();
        mBank.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                terminateAbility();
            }
        });
        mLeftBankFour.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                terminateAbility();
            }
        });

        mButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                terminateAbility();
            }
        });
        switch (position) {
            case 1:
                mCustomTitleBar1.setVisibility(Component.VISIBLE);
                mCustomTitleBar2.setVisibility(Component.HIDE);
                mCustomTitleBar3.setVisibility(Component.HIDE);
                mCustomTitleBar4.setVisibility(Component.HIDE);
                mCustomTitleBar5.setVisibility(Component.HIDE);
                mCustomTitleBar6.setVisibility(Component.HIDE);
                mCustomTitleBar7.setVisibility(Component.HIDE);
                mCustomTitleBar8.setVisibility(Component.HIDE);
                mCustomTitleBar9.setVisibility(Component.HIDE);
                mCustomTitleBar10.setVisibility(Component.HIDE);
                mCustomTitleBar11.setVisibility(Component.HIDE);
                break;
            case 2:
                mCustomTitleBar1.setVisibility(Component.HIDE);
                mCustomTitleBar2.setVisibility(Component.HIDE);
                mCustomTitleBar3.setVisibility(Component.HIDE);
                mCustomTitleBar4.setVisibility(Component.VISIBLE);
                mCustomTitleBar5.setVisibility(Component.HIDE);
                mCustomTitleBar6.setVisibility(Component.HIDE);
                mCustomTitleBar7.setVisibility(Component.HIDE);
                mCustomTitleBar8.setVisibility(Component.HIDE);
                mCustomTitleBar9.setVisibility(Component.HIDE);
                mCustomTitleBar10.setVisibility(Component.HIDE);
                mCustomTitleBar11.setVisibility(Component.HIDE);
                break;
            case 3:
                mCustomTitleBar1.setVisibility(Component.HIDE);
                mCustomTitleBar2.setVisibility(Component.HIDE);
                mCustomTitleBar3.setVisibility(Component.VISIBLE);
                mCustomTitleBar4.setVisibility(Component.HIDE);
                mCustomTitleBar5.setVisibility(Component.HIDE);
                mCustomTitleBar6.setVisibility(Component.HIDE);
                mCustomTitleBar7.setVisibility(Component.HIDE);
                mCustomTitleBar8.setVisibility(Component.HIDE);
                mCustomTitleBar9.setVisibility(Component.HIDE);
                mCustomTitleBar10.setVisibility(Component.HIDE);
                mCustomTitleBar11.setVisibility(Component.HIDE);
                break;
            case 4:
                mCustomTitleBar1.setVisibility(Component.HIDE);
                mCustomTitleBar2.setVisibility(Component.HIDE);
                mCustomTitleBar3.setVisibility(Component.HIDE);
                mCustomTitleBar4.setVisibility(Component.HIDE);
                mCustomTitleBar5.setVisibility(Component.HIDE);
                mCustomTitleBar6.setVisibility(Component.HIDE);
                mCustomTitleBar7.setVisibility(Component.HIDE);
                mCustomTitleBar8.setVisibility(Component.HIDE);
                mCustomTitleBar9.setVisibility(Component.HIDE);
                mCustomTitleBar10.setVisibility(Component.HIDE);
                mCustomTitleBar11.setVisibility(Component.HIDE);
                mDependentLayoutFour.setVisibility(Component.VISIBLE);
                break;
            case 5:
                mCustomTitleBar1.setVisibility(Component.HIDE);
                mCustomTitleBar2.setVisibility(Component.HIDE);
                mCustomTitleBar3.setVisibility(Component.HIDE);
                mCustomTitleBar4.setVisibility(Component.HIDE);
                mCustomTitleBar5.setVisibility(Component.VISIBLE);
                mCustomTitleBar6.setVisibility(Component.HIDE);
                mCustomTitleBar7.setVisibility(Component.HIDE);
                mCustomTitleBar8.setVisibility(Component.HIDE);
                mCustomTitleBar9.setVisibility(Component.HIDE);
                mCustomTitleBar10.setVisibility(Component.HIDE);
                mCustomTitleBar11.setVisibility(Component.HIDE);
                break;
            case 6:
                mCustomTitleBar1.setVisibility(Component.HIDE);
                mCustomTitleBar2.setVisibility(Component.HIDE);
                mCustomTitleBar3.setVisibility(Component.HIDE);
                mCustomTitleBar4.setVisibility(Component.HIDE);
                mCustomTitleBar5.setVisibility(Component.HIDE);
                mCustomTitleBar6.setVisibility(Component.VISIBLE);
                mCustomTitleBar7.setVisibility(Component.HIDE);
                mCustomTitleBar8.setVisibility(Component.HIDE);
                mCustomTitleBar9.setVisibility(Component.HIDE);
                mCustomTitleBar10.setVisibility(Component.HIDE);
                mCustomTitleBar11.setVisibility(Component.HIDE);
                break;
            case 7:
                mCustomTitleBar1.setVisibility(Component.HIDE);
                mCustomTitleBar2.setVisibility(Component.HIDE);
                mCustomTitleBar3.setVisibility(Component.HIDE);
                mCustomTitleBar4.setVisibility(Component.HIDE);
                mCustomTitleBar5.setVisibility(Component.HIDE);
                mCustomTitleBar6.setVisibility(Component.HIDE);
                mCustomTitleBar7.setVisibility(Component.VISIBLE);
                mCustomTitleBar8.setVisibility(Component.HIDE);
                mCustomTitleBar9.setVisibility(Component.HIDE);
                mCustomTitleBar10.setVisibility(Component.HIDE);
                mCustomTitleBar11.setVisibility(Component.HIDE);
                break;
            case 8:
                mCustomTitleBar1.setVisibility(Component.HIDE);
                mCustomTitleBar2.setVisibility(Component.HIDE);
                mCustomTitleBar3.setVisibility(Component.HIDE);
                mCustomTitleBar4.setVisibility(Component.HIDE);
                mCustomTitleBar5.setVisibility(Component.HIDE);
                mCustomTitleBar6.setVisibility(Component.HIDE);
                mCustomTitleBar7.setVisibility(Component.HIDE);
                mCustomTitleBar8.setVisibility(Component.VISIBLE);
                mCustomTitleBar9.setVisibility(Component.HIDE);
                mCustomTitleBar10.setVisibility(Component.HIDE);
                mCustomTitleBar11.setVisibility(Component.HIDE);
                break;
            case 9:
                mCustomTitleBar1.setVisibility(Component.HIDE);
                mCustomTitleBar2.setVisibility(Component.HIDE);
                mCustomTitleBar3.setVisibility(Component.HIDE);
                mCustomTitleBar4.setVisibility(Component.HIDE);
                mCustomTitleBar5.setVisibility(Component.HIDE);
                mCustomTitleBar6.setVisibility(Component.HIDE);
                mCustomTitleBar7.setVisibility(Component.HIDE);
                mCustomTitleBar8.setVisibility(Component.HIDE);
                mCustomTitleBar9.setVisibility(Component.HIDE);
                mCustomTitleBar10.setVisibility(Component.HIDE);
                mCustomTitleBar11.setVisibility(Component.HIDE);
                mDelPager.setVisibility(Component.VISIBLE);
                break;
            case 10:
                mCustomTitleBar1.setVisibility(Component.HIDE);
                mCustomTitleBar2.setVisibility(Component.HIDE);
                mCustomTitleBar3.setVisibility(Component.HIDE);
                mCustomTitleBar4.setVisibility(Component.HIDE);
                mCustomTitleBar5.setVisibility(Component.HIDE);
                mCustomTitleBar6.setVisibility(Component.HIDE);
                mCustomTitleBar7.setVisibility(Component.HIDE);
                mCustomTitleBar8.setVisibility(Component.HIDE);
                mCustomTitleBar9.setVisibility(Component.HIDE);
                mCustomTitleBar10.setVisibility(Component.VISIBLE);
                mCustomTitleBar11.setVisibility(Component.HIDE);
                break;
            case 11:
                mCustomTitleBar1.setVisibility(Component.HIDE);
                mCustomTitleBar2.setVisibility(Component.HIDE);
                mCustomTitleBar3.setVisibility(Component.HIDE);
                mCustomTitleBar4.setVisibility(Component.HIDE);
                mCustomTitleBar5.setVisibility(Component.HIDE);
                mCustomTitleBar6.setVisibility(Component.HIDE);
                mCustomTitleBar7.setVisibility(Component.HIDE);
                mCustomTitleBar8.setVisibility(Component.HIDE);
                mCustomTitleBar9.setVisibility(Component.HIDE);
                mCustomTitleBar10.setVisibility(Component.HIDE);
                mCustomTitleBar11.setVisibility(Component.VISIBLE);
                break;
        }
    }

    /**
     * 设置tabList
     */
    private void initData() {
        try {
            String[] tabsTitle = getResourceManager().getElement(ResourceTable.Strarray_tab_title).getStringArray();
            for (String title : tabsTitle) {
                TabList.Tab tab = tabList.new Tab(getContext());
                tab.setText(title);
                ShapeElement shapeElement = new ShapeElement();
                shapeElement.setRgbColor(new RgbColor(0, 0, 0, 0));
                tab.setBackground(shapeElement);
                tabList.addTab(tab);
            }
            tabList.selectTabAt(0);
            pageSlider.setProvider(new MainPagerAdapter(initPageSliderViewData()));
            pageSlider.setCurrentPage(0);
            pageSlider.setReboundEffect(true);
            pageSlider.setCentralScrollMode(true);
        } catch (IOException | NotExistException | WrongTypeException e) {
            e.printStackTrace();
        }
    }

    //数据
    private ArrayList<Component> initPageSliderViewData() {
        int pageSize = tabList.getTabCount();
        ArrayList<Component> pages = new ArrayList<>();
        ViewCreateHelper viewCreateHelper = new ViewCreateHelper(getContext());
        for (int i = 0; i < pageSize; i++) {
            pages.add(viewCreateHelper.createView(tabList.getTabAt(i).getText(), i));
        }
        return pages;
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
    }

    @Override
    public void onSelected(TabList.Tab tab) {
        pageSlider.setCurrentPage(tab.getPosition());
    }

    @Override
    public void onPageSliding(int i, float v, int i1) {
    }

    @Override
    public void onPageSlideStateChanged(int i) {
    }

    @Override
    public void onPageChosen(int pageSlideSelect) {
        if (tabList.getSelectedTab().getPosition() != pageSlideSelect) {
            tabList.selectTabAt(pageSlideSelect);
        }
    }

    @Override
    protected void onBackground() {
        super.onBackground();
    }

    @Override
    public void onUnselected(TabList.Tab tab) {
    }

    @Override
    public void onReselected(TabList.Tab tab) {
    }
}
